/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ThreadClient;

import InterfaceClient.Message;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author DELL
 */
public class ThreadMessages {

    public Socket soc;
    public String message = "";
    int port;
    String title;

    public ThreadMessages(int i) {
        this.port = i;
    }

    public Socket TaoKetNoi() {
        try {
            soc = new Socket("localhost", port);
            return soc;
        } catch (UnknownHostException ex) {
            return null;
        } catch (IOException ex) {
            return null;
        }
    }

    public void Listen(final Socket soc) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {                        
                        ObjectInputStream in = new ObjectInputStream(soc.getInputStream());
                        title = (String) in.readObject();
                        message = (String) in.readObject();
                        System.out.println(title);
                        Message ms=new Message(title, message);
                        ms.setVisible(true);
                        Thread.sleep(20000);
                        ms.setVisible(false);
                    }
                } catch (ClassNotFoundException ex) {
                  } catch (InterruptedException ex) {
                } catch (IOException e) {
                }
            }
        }).start();
    }  

}
